package com.example.springboot_es.controller;

import com.example.springboot_es.data.MonitorAlarmVO;
import com.example.springboot_es.dao.MonitorAlarmRepository;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;

@RestController
public class MonitorAlarmController {


    @Autowired
    private MonitorAlarmRepository monitorAlarmRepository;

    //============================elasticsearch
    /**
     * 1、根据id查
     * @param id
     * @return
     */
    @GetMapping("/esById/{id}")
    public Optional<MonitorAlarmVO> getBookById(@PathVariable Long id) {
        return monitorAlarmRepository.findById(id);
    }

    /**
     * 2、查  ++:全文检索（根据整个实体的所有属性，可能结果为0个）
     * @param key
     * @return
     */
    @GetMapping("/esByKey/{key}")
    public List<MonitorAlarmVO> testSearch(@PathVariable String key) {
        QueryStringQueryBuilder builder = new QueryStringQueryBuilder(key);
        Iterable<MonitorAlarmVO> searchResult = monitorAlarmRepository.search(builder);
        Iterator<MonitorAlarmVO> iterator = searchResult.iterator();
        List<MonitorAlarmVO> list = new ArrayList<MonitorAlarmVO>();
        while (iterator.hasNext()) {
            list.add(iterator.next());
        }
        return list;
    }

    // http://localhost:8080/es/getAll
    @GetMapping("/getAll")
    public List<MonitorAlarmVO> testSearch() {
        Iterable<MonitorAlarmVO> searchResult = monitorAlarmRepository.findAll();
        Iterator<MonitorAlarmVO> iterator = searchResult.iterator();
        List<MonitorAlarmVO> list = new ArrayList<MonitorAlarmVO>();
        while (iterator.hasNext()) {
            list.add(iterator.next());
        }
        return list;
    }

    /**
     * 3、查   +++：分页、分数、分域（结果一个也不少）
     * @param page
     * @param size
     * @return
     * @return
     */
    @GetMapping("/{page}/{size}/{key}")
    public List<MonitorAlarmVO> searchCity(@PathVariable Integer page, @PathVariable Integer size, @PathVariable String key) {

//        // 分页参数
//        Pageable pageable = new PageRequest(page, size);
//
//        // 分数，并自动按分排序
//        FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery(QueryBuilders.boolQuery().should(QueryBuilders.matchQuery("message", key))
//                .add(QueryBuilders.boolQuery()), ScoreFunctionBuilders.weightFactorFunction(1000))
//                .add(QueryBuilders.boolQuery()), ScoreFunctionBuilders.weightFactorFunction(100));
//
//        // 分数、分页
//        NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withPageable(pageable)
//                .withQuery(functionScoreQueryBuilder).build();
//
//        Page<MonitorAlarmVO> searchPageResults = monitorAlarmRepository.search(searchQuery);
//        return searchPageResults.getContent();
    return null;
    }

    /**
     * 4、增
     * @param monitorAlarmVO
     * @return
     */
    // http://localhost:8080/es/esInsert
    @PostMapping("/esInsert")
    public MonitorAlarmVO insertBook(MonitorAlarmVO monitorAlarmVO) {
        monitorAlarmRepository.save(monitorAlarmVO);
        return monitorAlarmVO;
    }

    /**
     * 5、删 id
     * @param id
     * @return
     */
    @DeleteMapping("/esDelete/{id}")
    public Optional<MonitorAlarmVO> insertBook(@PathVariable Long id) {
        Optional<MonitorAlarmVO> monitorAlarmVO = monitorAlarmRepository.findById(id);
        monitorAlarmRepository.deleteById(id);
        return monitorAlarmVO;
    }

    /**
     * 6、改
     * @param monitorAlarmVO
     * @return
     */
    @PutMapping("/esUpdate")
    public MonitorAlarmVO updateBook(MonitorAlarmVO monitorAlarmVO) {
        monitorAlarmRepository.save(monitorAlarmVO);
        return monitorAlarmVO;
    }

}
